﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HZ.Supervise.Web.Message
{
    public partial class WarningSetupForCompany : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindSort();
                BindList();
                //添加下拉框js改变事件
                ddlDrugSort.Attributes.Add("onchange","SelectChange()");
            }   
        }
        /// <summary>
        /// 绑定药品类别
        /// </summary>
        private void BindSort()
        {
            HZ.Supervise.BLL.BLL_Sort bllSort = new HZ.Supervise.BLL.BLL_Sort();
            DataTable dtSort = bllSort.GetSort();
            ddlDrugSort.DataSource = dtSort;
            ddlDrugSort.DataTextField = "SortName";
            ddlDrugSort.DataValueField = "SortID";
            ddlDrugSort.DataBind();
            //在类别下拉框位置0处添加一项“---请选择---”
            ddlDrugSort.Items.Insert(0, "---请选择---");
        }


        /// <summary>
        /// 制定消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnConfirmMessage_Click(object sender, EventArgs e)
        {
            //将数据存入数据库
            Model.MessageTypeDetail message = new HZ.Supervise.Model.MessageTypeDetail();
            message.AddTime = DateTime.Now;
            message.AreaId = HZ.Fun.CheckString(hfdRecivedArea.Value.Trim());
            message.AreaName = HZ.Fun.CheckString(txtArea.Text.Trim());
            message.Days = int.Parse(HZ.Fun.CheckString(txtTimeOfRange.Text.Trim()));
            message.DayType = ddlTime.SelectedValue;
            message.CompanyId = HZ.Fun.CheckString(hdfRecivedCompany.Value.Trim());
            message.CompanyName = HZ.Fun.CheckString(txtCompany.Text.Trim());
            message.SortId = HZ.Fun.CheckString(ddlDrugSort.SelectedValue);
            message.SortName = HZ.Fun.CheckString(ddlDrugSort.SelectedItem.Text);
            message.TypeId = 2;
            message.TypeKey = "";
            message.TypeName = "企业药品监控";
            message.UserId = CurrentAccount.UserID;
            message.AmountRight = decimal.Parse(HZ.Fun.CheckString(txtAmount2.Text.Trim()));
            message.AmountLeft = decimal.Parse(HZ.Fun.CheckString(txtAmount.Text.Trim()));
            message.AmountParentLeft = int.Parse(ddlAmountCompare.SelectedValue.ToString());
            message.AmountParentRight = int.Parse(ddlAmountCompare2.SelectedValue.ToString());
            message.CountLeft = double.Parse(HZ.Fun.CheckString(txtCount.Text.Trim()));
            message.CountRight = double.Parse(HZ.Fun.CheckString(txtCount2.Text.Trim().ToString()));
            message.CountParentLeft = int.Parse(ddlCountCompare.SelectedValue.ToString());
            message.CountParentRight = int.Parse(ddlCountCompare2.SelectedValue.ToString());
            message.ProductName = HZ.Fun.CheckString(txtDrug.Text.Trim());
            message.GoodsID = HZ.Fun.CheckString(hdfRecivedGoods.Value);
            message.Level = ddlLevel.SelectedItem.Text;
            message.Is_Work = false;//默认启用
            message.MessageDetailId = Guid.NewGuid().ToString();
            message.ConnectType = int.Parse(ddlConnectType.SelectedValue.ToString());
            
            IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();
            if (ibllMessage.IsExistMessageTypeDetail(message))
            {
                HZ.Web.WebUtil.Alert("规则重复");
                return;
            }
            int i = ibllMessage.InsertIntoMessageTypeDetail(message);
            if (i == 0)
            {
                HZ.Web.WebUtil.Alert("添加失败");
            }
            else
            {
                HZ.Web.WebUtil.Alert("添加成功");
                BindList();
                //清空
                txtAmount.Text = "";
                txtArea.Text = "";
                txtCount.Text = "";
                txtDrug.Text = "";
                txtCompany.Text = "";
                txtTimeOfRange.Text = "";
                hdfRecivedGoods.Value = "";
                hdfRecivedCompany.Value = "";
                hfdRecivedArea.Value = "";
                hfdRecivedAreaName.Value = "";
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        void BindList()
        {
            int t = 0;
            IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();
            Model.MessageTypeDetail messageTypeDetail = new HZ.Supervise.Model.MessageTypeDetail();
            messageTypeDetail.UserId = CurrentAccount.UserID;
            gvwWarningStart.DataSource = ibllMessage.GetMessageTypeDetail(messageTypeDetail, false, "company", AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            gvwWarningStart.DataBind();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }
        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwWarningStart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < gvwWarningStart.Rows.Count; i++)
            {
                string areaName = ((Label)(gvwWarningStart.Rows[i].Cells[2].FindControl("lblAreaName"))).Text.ToString();
                ((Label)(gvwWarningStart.Rows[i].Cells[2].FindControl("lblAreaName"))).Text = SubStr(areaName, 12);

                string hospitalName = ((Label)(gvwWarningStart.Rows[i].Cells[1].FindControl("lblHospitalName"))).Text.ToString();
                ((Label)(gvwWarningStart.Rows[i].Cells[1].FindControl("lblHospitalName"))).Text = SubStr(hospitalName, 12);

                string productName = ((Label)(gvwWarningStart.Rows[i].Cells[4].FindControl("lblProductName"))).Text.ToString();
                ((Label)(gvwWarningStart.Rows[i].Cells[4].FindControl("lblProductName"))).Text = SubStr(productName, 12);

                if (((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblCompare"))).Text.ToString() == "1" || ((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblCompare"))).Text.ToString() == "大于等于")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblCompare"))).Text = "大于等于";
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblCompare"))).Text = "小于等于";
                }
                if (((Label)(gvwWarningStart.Rows[i].Cells[9].FindControl("lblCompare2"))).Text.ToString() == "1" || ((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblCompare2"))).Text.ToString() == "大于等于")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[9].FindControl("lblCompare2"))).Text = "大于等于";
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[9].FindControl("lblCompare2"))).Text = "小于等于";
                }

                if (((Label)(gvwWarningStart.Rows[i].Cells[11].FindControl("lblAmountCompare"))).Text.ToString() == "1" || ((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblAmountCompare"))).Text.ToString() == "大于等于")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[11].FindControl("lblAmountCompare"))).Text = "大于等于";
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[11].FindControl("lblAmountCompare"))).Text = "小于等于";
                }

                if (((Label)(gvwWarningStart.Rows[i].Cells[13].FindControl("lblAmountCompare2"))).Text.ToString() == "1" || ((Label)(gvwWarningStart.Rows[i].Cells[7].FindControl("lblAmountCompare2"))).Text.ToString() == "大于等于")
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[13].FindControl("lblAmountCompare2"))).Text = "大于等于";
                }
                else
                {
                    ((Label)(gvwWarningStart.Rows[i].Cells[13].FindControl("lblAmountCompare2"))).Text = "小于等于";
                }
            }
        }
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="sString"></param>
        /// <param name="nLeng"></param>
        /// <returns></returns>
        string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            string sNewStr = sString.Substring(0, nLeng);
            sNewStr = sNewStr + "...";
            return sNewStr;
        }
        /// <summary>
        /// 删除预警
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelte_Click(object sender, EventArgs e)
        {
            string chkedids = Request.Form["chk"];
            int flag = 1;
            IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();
            foreach (var item in chkedids.Split(','))
            {
                flag = ibllMessage.DeleteMessageTypeDetail(item);
            }
            if (flag == 1)
            {
                HZ.Web.WebUtil.Alert("删除成功！");
            }
            else
            {
                HZ.Web.WebUtil.Alert("删除失败！");
            }
            BindList();
        }
        /// <summary>
        /// 启用预警
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnStart_Click(object sender, EventArgs e)
        {
            string chkedids = Request.Form["chk"];
            int flag = 1;
            IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();
            foreach (var item in chkedids.Split(','))
            {
                flag = ibllMessage.WarningSwitch(item, "1");
            }
            if (flag == 1)
            {
                HZ.Web.WebUtil.Alert("启用成功！");
            }
            else
            {
                HZ.Web.WebUtil.Alert("启用失败！");
            }
            BindList();
        }

        protected void gvwWarningStart_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwWarningStart.EditIndex = e.NewEditIndex;
            BindList();
        }

        protected void gvwWarningStart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwWarningStart.EditIndex = -1;
            BindList();
        }

        protected void gvwWarningStart_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();

                int rowIndex = e.RowIndex;
                string strCountLeft = ((TextBox)this.gvwWarningStart.Rows[rowIndex].FindControl("EditTxtCountLeft")).Text.Trim();
                string strCountRight = ((TextBox)this.gvwWarningStart.Rows[rowIndex].FindControl("EditTxtCountRight")).Text.Trim();
                string strAmountLeft = ((TextBox)this.gvwWarningStart.Rows[rowIndex].FindControl("EditTxtAmountLeft")).Text.Trim();
                string strAmountRight = ((TextBox)this.gvwWarningStart.Rows[rowIndex].FindControl("EditTxtAmountRight")).Text.Trim();
                string strDays = ((TextBox)this.gvwWarningStart.Rows[rowIndex].FindControl("EditTxtDays")).Text.Trim();
                if (string.IsNullOrEmpty(strCountLeft))
                {
                    HZ.Web.WebUtil.Alert("数量不能为空！", this);
                    ((TextBox)gvwWarningStart.Rows[rowIndex].FindControl("EditProjectRemak")).Focus();
                    return;
                }
                if (string.IsNullOrEmpty(strCountRight))
                {
                    HZ.Web.WebUtil.Alert("数量不能为空！", this);
                    ((TextBox)gvwWarningStart.Rows[rowIndex].FindControl("EditProjectRemak")).Focus();
                    return;
                }
                if (string.IsNullOrEmpty(strCountLeft))
                {
                    HZ.Web.WebUtil.Alert("金额不能为空！", this);
                    ((TextBox)gvwWarningStart.Rows[rowIndex].FindControl("EditProjectName")).Focus();
                    return;
                }
                if (string.IsNullOrEmpty(strAmountRight))
                {
                    HZ.Web.WebUtil.Alert("金额不能为空！", this);
                    ((TextBox)gvwWarningStart.Rows[rowIndex].FindControl("EditProjectName")).Focus();
                    return;
                }
                if (string.IsNullOrEmpty(strDays))
                {
                    HZ.Web.WebUtil.Alert("预警范围不能为空！", this);
                    ((TextBox)gvwWarningStart.Rows[rowIndex].FindControl("EditProjectName")).Focus();
                    return;
                }

                HZ.Supervise.Model.MessageTypeDetail messTypeDetail = new HZ.Supervise.Model.MessageTypeDetail();
                messTypeDetail.MessageDetailId = this.gvwWarningStart.DataKeys[rowIndex].Value.ToString();

                messTypeDetail.CountLeft = int.Parse(HZ.Fun.CheckString(strCountLeft));
                messTypeDetail.CountRight = int.Parse(HZ.Fun.CheckString(strCountRight));
                messTypeDetail.AmountLeft = int.Parse(HZ.Fun.CheckString(strAmountLeft));
                messTypeDetail.AmountRight = int.Parse(HZ.Fun.CheckString(strAmountRight));

                messTypeDetail.Days = int.Parse(HZ.Fun.CheckString(strDays));


                if (ibllMessage.UpdateMessageTypeDetail(messTypeDetail) == 1)
                {
                    //重新绑定列表
                    this.gvwWarningStart.EditIndex = -1;
                    BindList();
                }

                else
                {
                    HZ.Web.WebUtil.Alert("更新失败！");
                    BindList();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BLL.BLL_MessageTypeDetail a = new HZ.Supervise.BLL.BLL_MessageTypeDetail();
            a.InsertIntoMessageSend();
        }
    }
}
